# -*- coding: utf-8 -*-
# @Time    : 2024/3/16 16:45
# @Author  : floraachy
# @File    : repo_code_page
# @Software: PyCharm
# @Desc:

# 标准库导入
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.ui_utils.base_page import BasePage


class RepoCodePage(BasePage):

    @allure.step("点击【文件】按钮")
    def click_file_button(self):
        """
        点击'文件'按钮，显示新建文件，上传文件
        """
        self.click(locator="//a[text()='文件 ']")
        self.page.wait_for_load_state()

    @allure.step("点击【新建文件】按钮")
    def click_create_file_button(self):
        """
       点击'新建文件'按钮
       """
        self.click(locator="//a[text()='新建文件']")
        self.page.wait_for_load_state()

    @allure.step("点击【上传文件】按钮")
    def click_upload_file_button(self):
        """
       点击'上传文件'按钮
       """
        self.click(locator="//a[text()='上传文件']")

    @allure.step("输入文件名称或文件路径：{file_name}")
    def input_file_name(self, file_name):
        """
       输入文件名称或文件路径
       """
        self.input("//input[@placeholder='命名文件名称或文件路径']", file_name)
        # self.input("//div[@class='grid-item']//input[@placeholder='命名文件名称或文件路径']", file_name)

    @allure.step("输入文件内容：{file_content}")
    def input_file_content(self, file_content):
        """
       输入文件内容
       """
        self.page.wait_for_timeout(1000)
        elem = self.page.query_selector(
            selector="//textarea/parent::div/parent::div[contains(@class, 'CodeMirror-wrap')]")
        self.execute_js('''([elem, content]) => elem.CodeMirror.setValue(content);''',
                        [elem, file_content])

    @allure.step("输入commit信息：{commits}")
    def input_commit_info(self, commits):
        """
        输入commit信息
        """
        self.input("//textarea[@id='UserSubmitForm_desc']", commits)

    @allure.step("点击【提交变更】按钮")
    def submit_commit_button(self):
        """
        点击提交变更按钮
        """
        self.click("//span[text()='提交变更']/parent::button")

    @allure.step("上传代码库文件: {code_file}")
    def upload_code_file(self, code_file: str):
        """
        上传代码库文件
        """
        self.upload_file(locator="//div[@class='ant-upload-drag-container']/preceding-sibling::input",
                         file_path=code_file)

    @allure.step("代码库页面，点击最新一次的提交记录")
    def click_newest_commit(self):
        """
        代码库页面，点击最新一次的提交记录
        """
        self.click("//pre[@id='ptxt']//p")

    def click_look_file_button(self):
        """
        点击【查看文件】按钮
        """
        self.click("//span[text()='查看文件']")

    @allure.step("获取当前节点的commitId")
    def get_current_commit_id(self):
        """
        获取当前节点的commitId
        """
        return self.get_text("//label[text()='当前节点']/following-sibling::span")

    @allure.step("获取当前节点的commitId")
    def get_file_name(self):
        """
        获取复制图标旁的文件名称
        """
        return self.get_text("//i[@class='iconfont icon-fuzhiicon ml6']/preceding-sibling::span")

    @allure.step("切换到master分支")
    def switch_branch(self):
        """
        切换到master分支
        """
        self.click("//div[@class='branch-tagBox ant-dropdown-trigger']")
        self.click("//a[text()='master']")

    @allure.step("点击【编辑】图标")
    def click_edit_icon(self):
        """
        点击编辑图标
        """
        self.click("//i[@class='iconfont icon-bianji1 font-15 color-grey-6']")

    @allure.step("点击【删除】图标，并且二次确认提示点击【确定】按钮")
    def click_delete_icon_and_confirm(self):
        """
        点击删除图标
        """
        self.click("//i[@class='iconfont icon-shanchu font-15 color-grey-6']")
        self.page.wait_for_timeout(1000)
        self.click("//span[text()='确 定']")
